package cn.wh.product.service.impl;

import cn.wh.basic.util.BaiduAuditUtils;
import cn.wh.org.domain.Employee;
import cn.wh.org.mapper.EmployeeMapper;
import cn.wh.pet.domain.Pet;
import cn.wh.pet.domain.PetDetail;
import cn.wh.pet.domain.PetOnlineAuditLog;
import cn.wh.pet.mapper.PetOnlineAuditLogMapper;
import cn.wh.product.domain.Product;
import cn.wh.product.domain.ProductDetail;
import cn.wh.product.domain.ProductOnlineAuditLog;
import cn.wh.product.mapper.ProductCommentMapper;
import cn.wh.product.mapper.ProductDetailMapper;
import cn.wh.product.mapper.ProductMapper;
import cn.wh.product.mapper.ProductOnlineAuditLogMapper;
import cn.wh.product.service.IProductService;
import cn.wh.basic.service.impl.BaseServiceImpl;
import cn.wh.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yaosang
 * @since 2022-06-30
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductOnlineAuditLogMapper productOnlineAuditLogMapper;


    @Autowired
    private ProductDetailMapper productDetailMapper;

    @Override
    public void save(Product product) {
        productMapper.save(product);
        ProductDetail productDetail = product.getProductDetail();
        if(productDetail != null){
            productDetail.setProductId(product.getId());
            productDetailMapper.save(productDetail);
        }
    }

    @Override
    public void onsale(List<Long> ids) {
        for (Long id : ids) {
            Product product = productMapper.loadById(id);
            StringBuilder sb =  new StringBuilder();
            if(!BaiduAuditUtils.TextCensor(product.getName())){
                sb.append(product.getName()+"名字不合法！！");
            }
            /*//审核图片
            if(!BaiduAuditUtils.ImgCensor("http://123.207.27.208"+pet.getResources().split(",")[0])){
                sb.append(pet.getName()+"图片不合法！！");
            }*/
            //1.1 审核不通过？ 记录审核日志，跳过，下一个继续
            if(sb.toString().length()>0){
                ProductOnlineAuditLog productOnlineAuditLog = new ProductOnlineAuditLog();
                productOnlineAuditLog.setNote("你好");
                productOnlineAuditLog.setProductId(product.getId());
                productOnlineAuditLogMapper.save(productOnlineAuditLog);
            }
            //1.2 修改上架状态，记录上架时间
            Map<String,Object> map = new HashMap<>();
            map.put("id",id);
            map.put("onSaleTime",new Date());
            productMapper.onSale(map);
        }
    }

    /**
     * 下架
     * @param ids
     */
    @Override
    public void offsale(List<Long> ids) {
        Map<String,Object> map = new HashMap<>();
        map.put("ids",ids);
        map.put("offSaleTime",new Date());
        productMapper.offSale(map);
    }
}
